package org.grayrabbit.cms.publish.generate;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.util.FilePathSptUtil;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 2011-6-15 10:43:13
 */
public class ContentGenerater extends AbstractGenerater {
	
	public  static String htmlFileNameExt = ".html";
	public  static String htmlHiddenFileNameExt = "_.html";
	public  static String contentPath = "content";
	public  static String taxQuestionPath = "taxquestion";
	
	private Content content;
	private boolean isTax;
	private boolean isTaxQuestion;
	private Category taxPath;
	private Site site;


	public ContentGenerater(Configuration configuration, Site site, String path,Category taxPath,boolean isTaxQuestion,Generatable generatable,Admin admin) {
		super(configuration, path,generatable,admin);
		this.taxPath = taxPath;
		this.isTaxQuestion = isTaxQuestion;
		this.site = site;
	}
	
	public ContentGenerater(Configuration configuration,  Site site, String path, Content content,Generatable generatable,Admin admin) {
		super(configuration, path,generatable,admin);
		this.content = content;
		this.site=site;
	}
	
	public ContentGenerater(Configuration configuration,  Site site, String path, Content content,boolean isTax,Generatable generatable,Admin admin) {
		super(configuration, path,generatable,admin);
		this.content = content;
		this.isTax = isTax;
		this.site = site;
	}

	@Override
	protected File getTargetFile() {
		return new File(getFilePath(content));
	}

	@Override
	protected void initRootMap() {		 
		rootMap.put("content", content);
		
		if(content!=null){
			rootMap.put("category", content.getCategory());
			if(content.getCategory().getSite()!=null){
				rootMap.put("site", content.getCategory().getSite());
			}else{
				rootMap.put("site", site);
			}
		}else{
			rootMap.put("category", taxPath);
			if(content.getCategory().getSite()!=null){
				rootMap.put("site", content.getCategory().getSite());
			}else{
				rootMap.put("site", site);
			}
		}
		
	}
	
	private String getFilePath(Content content){
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
		String path = "";
		if(content.getPublishTime()==null){
			content.setPublishTime(new Date());
		}
		if(isTax){
			path = FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+content.getCategory().getSite().getSiteShortName()+FilePathSptUtil.padLeftSpt(contentPath)+FilePathSptUtil.padLeftSpt(sdf1.format(content.getPublishTime())+FilePathSptUtil.padLeftSpt(sdf2.format(content.getPublishTime())))+FilePathSptUtil.padLeftSpt(content.getContentId())+htmlHiddenFileNameExt;
		}else{
			path = FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+content.getCategory().getSite().getSiteShortName()+FilePathSptUtil.padLeftSpt(contentPath)+FilePathSptUtil.padLeftSpt(sdf1.format(content.getPublishTime()==null?new Date():content.getPublishTime()))+FilePathSptUtil.padLeftSpt(sdf2.format(content.getPublishTime()))+FilePathSptUtil.padLeftSpt(content.getContentId())+htmlFileNameExt;
		}
		return path;
	}
	
	@Override
	protected void logInfo(PublishTask publishTask,boolean success,Throwable t) {
		PublishLog publishLog = new PublishLog();
		publishLog.setHtmlPath(getTargetFile().toString());
		publishLog.setIsError(Boolean.valueOf(!success));
		publishLog.setObjectId(content.getContentId());
		if(success){
			publishLog.setPublishInfo("生成成功");
			generatable.saveContent(content, admin, DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		}else{
			if(t!=null){
				publishLog.setPublishInfo("生成失败:"+t.getMessage());
			}else{
				publishLog.setPublishInfo("生成失败");
			}
		}
		publishLog.setPublishStatus(PublishTask.STATUS_FINISHED);
		publishLog.setPublishTime(new Date());
		publishLog.setPublishType("content");
		publishLog.setTemplatePath(this.path);
		publishLog.setPublishTask(publishTask);
		generatable.savePublishLog(publishLog);
		calcTaskInfo(publishTask,success);
		
	}

	public String toString(){
		return "content:"+content.getContentId();
	}

}